/**
 * 作者: hxl
 * 日期: 2024-12-25 10:17:33
 * 备注: 用户七天乐活动表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserActDays;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserActModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.act_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = true;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 玩家id
    protected long actId; // 活动id
    protected String buyGift = ""; // 已购买的礼包
    protected String integralReward = ""; // 进度宝箱
    protected int integralScore; // 进度积分
    protected long loginTime; // 登录时间
    protected int groupId; // 期数ID
    protected long startTime; // 开始时间
    protected long endTime; // 结束时间

    public void reset() {
    }

    protected String getKey() {
        return "UserAct." + userId + "." + actId;
    }

    protected String getListKey() {
        return "UserAct." + userId;
    }

    protected String getListItemKey() {
        return actId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_act_days"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 玩家id
        obj.put("actId", actId); // 活动id
        obj.put("buyGift", buyGift); // 已购买的礼包
        obj.put("integralReward", integralReward); // 进度宝箱
        obj.put("integralScore", integralScore); // 进度积分
        obj.put("loginTime", loginTime); // 登录时间
        obj.put("groupId", groupId); // 期数ID
        obj.put("startTime", startTime); // 开始时间
        obj.put("endTime", endTime); // 结束时间
        return obj;
    }

    public PBUserActDays.Builder takeInitBuilder() {
        PBUserActDays.Builder sb = PBUserActDays.newBuilder();
        if (this.actId != 0) {
            sb.setActId(this.actId);
        }
        if (this.buyGift != null && this.buyGift.length() > 0) {
            JsonArray list = new JsonArray(this.buyGift);
            for(int i=0; i<list.size(); i++) {
                sb.addBuyGift(list.getInteger(i));
            }
        }
        if (this.integralReward != null && this.integralReward.length() > 0) {
            JsonArray list = new JsonArray(this.integralReward);
            for(int i=0; i<list.size(); i++) {
                sb.addIntegralReward(list.getInteger(i));
            }
        }
        if (integralScore != 0) {
            sb.setIntegralScore(integralScore);
        }
        if (startTime != 0) {
            sb.setStartTime(startTime);
        }
        if (endTime != 0) {
            sb.setEndTime(endTime);
        }
        return sb;
    }

    public PBUserActDays.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserActDays.Builder sb = PBUserActDays.newBuilder();
        if (isFieldUpdate("actId")){
            sb.setActId(actId);
        }
        if (isFieldUpdate("buyGift")) {
            if (this.buyGift != null && this.buyGift.length() > 0) {
                JsonArray list = new JsonArray(this.buyGift);
                for(int i=0; i<list.size(); i++) {
                    sb.addBuyGift(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearBuyGift();
                }
            } else {
                sb.clearBuyGift();
            }
        }
        if (isFieldUpdate("integralReward")) {
            if (this.integralReward != null && this.integralReward.length() > 0) {
                JsonArray list = new JsonArray(this.integralReward);
                for(int i=0; i<list.size(); i++) {
                    sb.addIntegralReward(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearIntegralReward();
                }
            } else {
                sb.clearIntegralReward();
            }
        }
        if (isFieldUpdate("integralScore")) {
            sb.setIntegralScore(integralScore);
        }
        if (isFieldUpdate("startTime")) {
            sb.setStartTime(startTime);
        }
        if (isFieldUpdate("endTime")) {
            sb.setEndTime(endTime);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public long getActId() {
        return actId;
    }

    public void putActId(long actId) {
        if (this.actId != actId) {
            this.actId = actId;
            updateRedisHashField(getKey(), "actId", this.actId);
        }
    }

    public void addActId(long value) {
        this.actId += value;
        updateRedisHashField(getKey(), "actId", this.actId);
    }

    public JsonArray getBuyGiftArray() {
        if (this.buyGift != null && this.buyGift.length() > 0) {
            return new JsonArray(this.buyGift);
        } else {
            return new JsonArray();
        }
    }

    public String getBuyGift() {
        return buyGift;
    }

    public void putBuyGift(String buyGift) {
        if (!this.buyGift.equals(buyGift)) {
            this.buyGift = buyGift;
            updateRedisHashField(getKey(), "buyGift", this.buyGift);
        }
    }

    public JsonArray getIntegralRewardArray() {
        if (this.integralReward != null && this.integralReward.length() > 0) {
            return new JsonArray(this.integralReward);
        } else {
            return new JsonArray();
        }
    }

    public String getIntegralReward() {
        return integralReward;
    }

    public void putIntegralReward(String integralReward) {
        if (!this.integralReward.equals(integralReward)) {
            this.integralReward = integralReward;
            updateRedisHashField(getKey(), "integralReward", this.integralReward);
        }
    }

    public int getIntegralScore() {
        return integralScore;
    }

    public void putIntegralScore(int integralScore) {
        if (this.integralScore != integralScore) {
            this.integralScore = integralScore;
            updateRedisHashField(getKey(), "integralScore", this.integralScore);
        }
    }

    public void addIntegralScore(int value) {
        this.integralScore += value;
        updateRedisHashField(getKey(), "integralScore", this.integralScore);
    }

    public long getLoginTime() {
        return loginTime;
    }

    public void putLoginTime(long loginTime) {
        if (this.loginTime != loginTime) {
            this.loginTime = loginTime;
            updateRedisHashField(getKey(), "loginTime", this.loginTime);
        }
    }

    public void addLoginTime(long value) {
        this.loginTime += value;
        updateRedisHashField(getKey(), "loginTime", this.loginTime);
    }

    public int getGroupId() {
        return groupId;
    }

    public void putGroupId(int groupId) {
        if (this.groupId != groupId) {
            this.groupId = groupId;
            updateRedisHashField(getKey(), "groupId", this.groupId);
        }
    }

    public void addGroupId(int value) {
        this.groupId += value;
        updateRedisHashField(getKey(), "groupId", this.groupId);
    }

    public long getStartTime() {
        return startTime;
    }

    public void putStartTime(long startTime) {
        if (this.startTime != startTime) {
            this.startTime = startTime;
            updateRedisHashField(getKey(), "startTime", this.startTime);
        }
    }

    public void addStartTime(long value) {
        this.startTime += value;
        updateRedisHashField(getKey(), "startTime", this.startTime);
    }

    public long getEndTime() {
        return endTime;
    }

    public void putEndTime(long endTime) {
        if (this.endTime != endTime) {
            this.endTime = endTime;
            updateRedisHashField(getKey(), "endTime", this.endTime);
        }
    }

    public void addEndTime(long value) {
        this.endTime += value;
        updateRedisHashField(getKey(), "endTime", this.endTime);
    }

}